package com.accp.hotevent.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.accp.hotevent.entity.User;
import com.accp.hotevent.req.LoginUserRequest;
import com.accp.hotevent.req.RegisterUserRequest;
import com.accp.hotevent.resp.CommonResp;
import com.accp.hotevent.resp.LoginUserResp;
import com.accp.hotevent.service.UserService;
import com.accp.hotevent.util.LoggedUserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/login")
    public CommonResp<LoginUserResp> login(LoginUserRequest userRequest){
        User userdb = userService.login(userRequest.getEmail(), userRequest.getPassword());
        //HashMap<String,Object> map = new HashMap<>();
        CommonResp<LoginUserResp> commonResp = new CommonResp<>();
        if(userdb != null){
            LoginUserResp userResp = new LoginUserResp();
            BeanUtils.copyProperties(userdb,userResp);
            String token = IdUtil.simpleUUID().toUpperCase(Locale.ROOT);
            log.info("生成token:{}",token);
            userResp.setToken(token);
            //把token作为key,user作为val存入redis
            redisTemplate.opsForValue().set(token,userdb,30, TimeUnit.MINUTES);

            commonResp.setSuccess(true);
            commonResp.setData(userResp);
        }else{
            commonResp.setSuccess(false);
            commonResp.setMessage("账号或密码错误");
        }
        return commonResp;
    }


    @PostMapping("/register")
    public CommonResp register(@RequestBody RegisterUserRequest registerUserRequest){
        String verifyCodeDB = (String) redisTemplate.opsForValue().get(registerUserRequest.getD());
        if(verifyCodeDB == null){
            throw new RuntimeException("验证码过期");
        }else if(!verifyCodeDB.equals(registerUserRequest.getVerifyCode())){
            throw new RuntimeException("验证码输入有误");
        }
        User user = BeanUtil.copyProperties(registerUserRequest,User.class);
        userService.add(user);
        CommonResp commonResp = new CommonResp();
        commonResp.setSuccess(true);
        return commonResp;
    }

    @PostMapping("/avatar")
    public CommonResp<String> uploadAvatar(MultipartFile avatar){
        User user = LoggedUserContext.getCurrentLoggedUser();
        System.out.println("uploadAvatar...");
        try {
            byte[] imageBytes = avatar.getBytes();
            String base64String = Base64.getEncoder().encodeToString(imageBytes);
            System.out.println(base64String);
            user.setBase64Data(base64String);
            userService.update(user);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }

        // 对字节数组进行Base64编码

        CommonResp<String> commonResp = new CommonResp();
        commonResp.setSuccess(true);
        commonResp.setData(user.getBase64Data());
        return commonResp;
    }
}
